define(function () {

    var self, app = win.app, user;

    var Wttd = app.Module.extend({

        init: function () {
            self = this;

            self.data = {
                ESAA001_NAME: '',
                ESAA001: '',
                ESAA003: '',
                ESAA005: '',
                ESAA006: '',
                ESAA007: '',
                ESAA008: '',
                ESAA009: '',
                imgs: [],
                ESAA005List: []
            };
            self._super({
                _tag: 'wttd',
                _readyBefore: function () {
                    self.getEssaData();
                    self.getCpxData();
                }
            }, win);

        },
        subData: function () {
            var _data = self.data.sub;
            if (!_data.GWAA002 || _data.GWAA002 == '') {
                app.toast('姓名不能为空');
                return;
            }
            if (!_data.GWAA005 || _data.GWAA005 == '') {
                app.toast('出生日期不能为空');
                return;
            }
            if (!_data.GWAA004 || _data.GWAA004 == '') {
                app.toast('手机号不能为空');
                return;
            }
            _data.GWAA001 = user.id;
            app.ajax({
                xurl: "api/wx/user/registerUser",
                loading: true,
                xdata: _data,
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '注册成功！',
                            ok: function () {
                                app.back(1);
                            }
                        });
                    } else {
                        app.alert({
                            msg: result.data.appmsg,
                            ok: function () {
                                app.back(1);
                            }
                        });
                    }
                }
            });
        },
        getEssaData: function () {
            app.ajax({
                xurl: "api/wttd/getEsaaData",
                xdata: {},
                cb: function (result) {
                    if (result.data.code == 200 && result.data.data.length > 0) {
                        self.data.ESAA001 = result.data.data[0].ESAA001;
                        self.data.ESAA001_NAME = result.data.data[0].ESAA001_NAME;
                    }
                }
            });
        },
        getCpxData: function () {
            app.ajax({
                xurl: "api/wttd/getCpxData",
                xdata: {},
                cb: function (result) {
                    if (result.data.code == 200) {
                        self.data.ESAA005List = result.data.data;
                    }
                }
            });
        },
        addData: function () {
            if(self.data.ESAA001 == '' || self.data.ESAA001 == null){
                app.toast('请选择客户');
                return;
            }
            if(self.data.ESAA003 == '' || self.data.ESAA003 == null){
                app.toast('请选择问题类型');
                return;
            }
            if(self.data.ESAA005 == '' || self.data.ESAA005 == null){
                app.toast('请选择产品线');
                return;
            }
            if(self.data.ESAA006 == '' || self.data.ESAA006 == null){
                app.toast('请选择ERP产品线');
                return;
            }
            if(self.data.ESAA007 == '' || self.data.ESAA007 == null){
                app.toast('请输入作业名称');
                return;
            }
            if(self.data.ESAA008 == '' || self.data.ESAA008 == null){
                app.toast('请输入问题描述');
                return;
            }
            app.showLoading();
            // self.data.imgs = [{img:'img://storage/emulated/0/Tencent/WeixinWork/tempimagecache/168885305125743',
            //     name:'15813900.jpg',index:'0',
            //     id:'img://storage/emulated/0/Tencent/WeixinWork/tempimagecache/168885305125743'}];
            if (self.data.imgs.length > 0) {
                app.uploadLocalIds({
                    ids: self.data.imgs,
                    cb: function (imgArr) {
                        var imgurl = "";
                        for (var i = 0; i < imgArr.length; i++) {
                            imgurl += (imgArr[i].name + ",");
                        }
                        self.data.ESAA009=imgurl.substring(0,imgurl.length-1);
                        app.ajax({
                            xurl: 'api/wttd/addData',
                            xdata: self.data,
                            loading: true,
                            cb: function (result) {
                                app.hideLoading();
                                if (result.ok) {
                                    app.toast('新增成功！');
                                    app.back(1);
                                } else {
                                    app.toast(result.data.message);
                                }
                            }
                        });
                    }
                });
                /*app.uploadBase64({
                    imgs:self.data.imgs,
                    cb: function (imgArr) {
                        var imgurl = "";
                        for (var i = 0; i < imgArr.length; i++) {
                            imgurl += (imgArr[i].name + ",");
                        }
                        self.data.ESAA009=imgurl.substring(0,imgurl.length-1);
                        app.ajax({
                            xurl: 'api/wttd/addData',
                            xdata: self.data,
                            loading: true,
                            cb: function (result) {
                                app.hideLoading();
                                if (result.ok) {
                                    app.toast('新增成功！');
                                    app.back(1);
                                } else {
                                    app.toast(result.data.message);
                                }
                            }
                        });
                    }
                });*/

            }else{
                app.ajax({
                    xurl: 'api/wttd/addData',
                    xdata: self.data,
                    loading: true,
                    cb: function (result) {
                        app.hideLoading();
                        if (result.ok) {
                            app.toast('新增成功！');
                            app.back(1);
                        } else {
                            app.toast(result.data.message);
                        }
                    }
                });
            }
        },
        _lazy: function () { // 延迟300ms加载
            user = {
                id: app.cookie('_WX_ACCOUNT_'),
                name: app.cookie('_WX_USERNAME_'),
                mobile: app.cookie('_WX_MOBILE_')
            };
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
            app.taps.call(win, [
                {
                    o: 'btn_save',
                    cb: function (e) {
                        self.addData();
                    },
                    confirm: {
                        msg: '确认现在提交？'
                    }
                }
            ]);
        },
        _ready: function () { // dom载入完成后执行
            var vue = new Vue({
                el: doc.querySelector('.mui-content'),
                data: self.data,
                methods: {
                    showGwak: function () {
                        app.openPage({
                            src: "wttd/customers.html",
                            argsKey: 'customers',
                            args: {
                                cb: function (data) {
                                    self.data.ESAA001 = data.ESAA001;
                                    self.data.ESAA001_NAME = data.ESAA001_NAME;
                                }
                            }
                        });
                    },
                    submit: function () {
                        if(self.data.ESAA001 == '' || self.data.ESAA001 == null){
                            app.toast('请选择客户');
                            return;
                        }
                        if(self.data.ESAA003 == '' || self.data.ESAA003 == null){
                            app.toast('请选择问题类型');
                            return;
                        }
                        if(self.data.ESAA005 == '' || self.data.ESAA005 == null){
                            app.toast('请选择产品线');
                            return;
                        }
                        if(self.data.ESAA006 == '' || self.data.ESAA006 == null){
                            app.toast('请选择ERP产品线');
                            return;
                        }
                        if(self.data.ESAA007 == '' || self.data.ESAA007 == null){
                            app.toast('请输入作业名称');
                            return;
                        }
                        if(self.data.ESAA008 == '' || self.data.ESAA008 == null){
                            app.toast('请输入问题描述');
                            return;
                        }
                        self.addData();
                    },
                    img_add: function (re) {
                        app.chooseImageNew(function (imgs) {
                            for (var i = 0; i < imgs.length; i++) {
                                self.data.imgs.push({
                                    img: imgs[i].img,
                                    id: imgs[i].localId
                                })
                            }
                        });
                        // app.chooseImageNew(function (imgs) {
                        //     self.data.imgs.pushArray(imgs);
                        // });
                    },
                    delimg: function (i) {
                        self.data.imgs.removei(i);
                    }
                }
            });
        }
    });

    new Wttd();

});